package com.rzjm.exercise.sys.system.role.service.impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

import com.rzjm.exercise.common.base.service.impl.BaseServiceImpl;
import com.rzjm.exercise.common.utils.RightsHelper;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.sys.system.role.dao.RoleDao;
import com.rzjm.exercise.sys.system.role.domain.RoleDO;
import com.rzjm.exercise.sys.system.role.service.RoleService;
import com.rzjm.exercise.sys.system.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleDao, RoleDO> implements RoleService {

    @Resource
    RoleDao roleMapper;
    @Resource
    UserDao userMapper;

    @Override
    public List<RoleDO> list() {
        List<RoleDO> roles = roleMapper.selectByMap(new HashMap<>(16));
        return roles;
    }

    @Override
    public int update(RoleDO role) {
        List<Long> menuIds = role.getMenuIds();
        String menuIdsStr = StringUtils.listToString(menuIds,',');
        BigInteger rights = RightsHelper.sumRights(RightsHelper.str2StrArray(menuIdsStr));
        role.setRights(rights.toString());
        int r = roleMapper.updateById(role);
        return r;
    }
}
